package cayte.xiami.in.ad;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import cayte.xiami.in.D;
import cayte.xiami.in.R;
import cayte.xiami.in.helper.Check;
import cayte.xiami.in.helper.SplashBgHelper;
import cayte.xiami.in.ui.CheckinActivity;
import cn.domob.android.ads.DomobSplashAd;
import cn.domob.android.ads.DomobSplashAdListener;

import com.umeng.analytics.MobclickAgent;

public class SplashScreen extends Activity {

	public static final String PUBLISHER_ID = "56OJzXc4uNaiJNofJG";

	private DomobSplashAd splashAd;

	private View layout;
	private ImageView bg;

	private static final int JUMP_TIME = 1000;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MobclickAgent.onError(this);
		setContentView(R.layout.splash);

		layout = findViewById(R.id.layout);
		bg = (ImageView) findViewById(R.id.bg);
		Bitmap bitmap = new SplashBgHelper(this).getBg();
		if (bitmap == null) {
			bg.setScaleType(ScaleType.CENTER_CROP);
			bg.setBackgroundResource(R.color.black);
			bg.setImageResource(R.drawable.splash);
		} else {
			bg.setScaleType(ScaleType.CENTER_INSIDE);
			bg.setBackgroundResource(R.color.black);
			bg.setImageBitmap(bitmap);
		}

		MobclickAgent.setSessionContinueMillis(60000);
		MobclickAgent.updateOnlineConfig(this);
		try {
			splashAd = new DomobSplashAd(this, PUBLISHER_ID);
		} catch (Exception e) {
			// TODO: handle exception
		}
		splashAd.setSplashAdListener(new DomobSplashAdListener() {
			@Override
			public void onSplashPresent() {
				Log.i("cayte", "onSplashStart");
			}

			@Override
			public void onSplashDismiss() {
				Log.i("cayte", "onSplashClosed");
				jumpNow();
			}

			@Override
			public void onSplashLoadFailed() {
				Log.i("cayte", "onSplashLoadFailed");
				jump(JUMP_TIME);
			}
		});

		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				if (D.OWNER) {
					jump(JUMP_TIME);
				} else {
					boolean isSplashAdReady = false;
					String adSplash = Check.ON;
					try {
						adSplash = MobclickAgent.getConfigParams(
								SplashScreen.this, "ad_splash");
						isSplashAdReady = splashAd.isSplashAdReady();
					} catch (Exception e) {
						// TODO: handle exception
					}
					if (isSplashAdReady && adSplash.equals(Check.ON)) {
						splashAd.splash(SplashScreen.this);
					} else {
						jump(JUMP_TIME);
					}

				}
			}
		}, JUMP_TIME);

		//
		AdUtil.splash(this);

		Animation show = new AlphaAnimation(0.1f, 1f);
		show.setDuration(900);
		layout.startAnimation(show);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}

	private void jump(long ms) {
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				Animation hide = new AlphaAnimation(1f, 0.1f);
				hide.setDuration(900);
				hide.setFillAfter(true);
				hide.setAnimationListener(new AnimationListener() {

					@Override
					public void onAnimationStart(Animation animation) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onAnimationRepeat(Animation animation) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onAnimationEnd(Animation animation) {
						// TODO Auto-generated method stub
						jumpNow();
					}
				});
				layout.startAnimation(hide);
			}
		}, ms);
	}

	private void jumpNow() {
		// TODO Auto-generated method stub
		startActivity(new Intent(SplashScreen.this, CheckinActivity.class));
		overridePendingTransition(0, 0);
		finish();
	}

}
